﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Borg.AttributeAnalyze
{
    [Serializable]
    public abstract class MSBuildResultBase
    {
        public static IEnumerable<MSBuildResultBase> Deserialize(string[] data)
        {
            var resultBases = new List<MSBuildResultBase>();

            foreach (var strs in data.Select(item => item.Split(new char[] { ';' })))
            {
                switch (strs.Length)
                {
                    case 5:
                        var buildResult =
                            new MSBuildResult
                                {
                                    Count = int.Parse(strs[0]),
                                    Description = Encoding.Default.GetString(Convert.FromBase64String(strs[1])),
                                    File = Encoding.Default.GetString(Convert.FromBase64String(strs[2])),
                                    SubCategory = Encoding.Default.GetString(Convert.FromBase64String(strs[3])),
                                    Typ = (MessageType) int.Parse(strs[4])
                                };
                        resultBases.Add(buildResult);
                        break;

                    case 9:
                        var resultExt =
                            new MSBuildResultExt
                                {
                                    ColumnEnd = int.Parse(strs[0]),
                                    ColumnStart = int.Parse(strs[1]),
                                    Count = int.Parse(strs[2]),
                                    Description = Encoding.Default.GetString(Convert.FromBase64String(strs[3])),
                                    File = Encoding.Default.GetString(Convert.FromBase64String(strs[4])),
                                    LineEnd = int.Parse(strs[5]),
                                    LineStart = int.Parse(strs[6]),
                                    SubCategory = Encoding.Default.GetString(Convert.FromBase64String(strs[7])),
                                    Typ = (MessageType) int.Parse(strs[8])
                                };
                        resultBases.Add(resultExt);
                        break;
                }
            }

            return resultBases;
        }

        public static byte[] Serialize(IEnumerable<MSBuildResultBase> data)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new StreamWriter(stream))
                {
                    foreach (var resultBase in data)
                    {
                        if (resultBase is MSBuildResultExt)
                        {
                            var ext = resultBase as MSBuildResultExt;

                            writer.WriteLine("{0};{1};{2};{3};{4};{5};{6};{7};{8}",
                                             new object[]
                                                 {
                                                     ext.ColumnEnd,
                                                     ext.ColumnStart,
                                                     ext.Count,
                                                     Convert.ToBase64String(
                                                         Encoding.Default.GetBytes(resultBase.Description)),
                                                     Convert.ToBase64String(Encoding.Default.GetBytes(ext.File)),
                                                     ext.LineEnd,
                                                     ext.LineStart,
                                                     Convert.ToBase64String(
                                                         Encoding.Default.GetBytes(resultBase.SubCategory)),
                                                     (int) resultBase.Typ
                                                 });
                        }
                        else
                            writer.WriteLine("{0};{1};{2};{3};{4}",
                                             new object[]
                                                 {
                                                     resultBase.Count,
                                                     Convert.ToBase64String(
                                                         Encoding.Default.GetBytes(resultBase.Description)),
                                                     Convert.ToBase64String(Encoding.Default.GetBytes(resultBase.File)),
                                                     Convert.ToBase64String(
                                                         Encoding.Default.GetBytes(resultBase.SubCategory)),
                                                     (int) resultBase.Typ
                                                 });
                    }
                }

                stream.Flush();
                return stream.GetBuffer();
            }
        }

        public abstract override string ToString();

        public int Count { get; set; }

        public string Description { get; set; }

        public string File { get; set; }

        public string SubCategory { get; set; }

        public MessageType Typ { get; set; }
    }
}

